package com.android.porttallymansystem.ui.adapter

import android.content.Intent
import android.text.Layout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.android.porttallymansystem.PTSApplication
import com.android.porttallymansystem.R
import com.android.porttallymansystem.logic.model.Work
import com.android.porttallymansystem.ui.activity.WorkActivity

class WorkAdapter(private val workList: List<Work>) :
    RecyclerView.Adapter<WorkAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val title: TextView = view.findViewById(R.id.tv_work_brief_title)
        val deadLine: TextView = view.findViewById(R.id.tv_work_brief_time)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_work, parent, false)
        val holder = ViewHolder(view)

        holder.itemView.setOnClickListener {
            val intent = Intent(parent.context, WorkActivity::class.java)
            intent.putExtra("workId", workList[holder.adapterPosition].id)
            parent.context.startActivity(intent)
        }

        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val work = workList[position]
        holder.title.text = work.title
        holder.deadLine.text = String.format(
            PTSApplication.context.resources.getString(R.string.time_2),
            work.yearOfDeadLine,
            work.monthOfDeadLine,
            work.dayOfDeadLine
        )
        if (workList[position].isFinished) {
            holder.title.setTextColor(PTSApplication.context.getColor(R.color.gray))
        } else {
            holder.title.setTextColor(PTSApplication.context.getColor(R.color.black))
        }
    }

    override fun getItemCount() = workList.size
}